package io.techery.progresshint.addition;

import ohos.agp.components.Component;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public final class ViewUtil {
    private static Display display;

    /**
     * 初始化
     *
     * @param context 上下文
     */
    public static void initContext(Context context) {
        display = DisplayManager.getInstance().getDefaultDisplay(context).get();
    }

    /**
     * getRelativeTop
     *
     * @param view view
     * @param container container
     * @return 距离顶部距离
     */
    static int getRelativeTop(Component view, Component container) {
        if (view.getComponentParent() == container) {
            return view.getTop();
        } else {
            return view.getTop() + getRelativeTop((Component) view.getComponentParent(), container);
        }
    }

    /**
     * 获取屏幕宽度
     *
     * @return 屏幕宽度
     */
    public static int getDisplayWidthInPx() {
        return display.getAttributes().width;
    }

    /**
     * 获取屏幕高度
     *
     * @return 屏幕高度
     */
    public static int getDisplayHeightInPx() {
        return display.getAttributes().height;
    }
}
